package com.magoldeditor.model;

import java.io.FileNotFoundException;
import java.util.HashMap;
import org.w3c.dom.Node;

import com.magoldeditor.at.MaEditorXMLException;
import com.magoldeditor.model.entity.Battery;
import com.magoldeditor.model.entity.Bulb;
import com.magoldeditor.model.entity.Cloud;
import com.magoldeditor.model.entity.Ejector;
import com.magoldeditor.model.entity.Fan;
import com.magoldeditor.model.entity.IEntity;
import com.magoldeditor.model.entity.IEntityFactory;
import com.magoldeditor.model.entity.MaEditorEntityFactory;
import com.magoldeditor.model.entity.PipeEntrance;
import com.magoldeditor.util.xml.ContentXMLReader;
import com.magoldeditor.util.xml.IContentReader;
import com.magoldeditor.view.ViewContants;

/**
 * 
 * @author Velconia
 * @version v.1.5
 * 
 */
public final class ModelContants implements IContentReader {
	
	/**
	 * public part
	 */
	public static final String ID_NAME = "id";
	public static final String ENTITY_CONTANTS_PATH = "entitycontants.xml";
	public static final String CONTANTS_FILE_NAME = ViewContants.CONFIG_ASSERT + "entitycontants.xml";
	public static final String IMAGE_ASSERT = ViewContants.IMAGE_ASSERT + "model/";
	public static final String X_NAME = "x";
	public static final String Y_NAME = "y";
	public static final String WIdTH_NAME = "width";
	public static final String HEIGHT_NAME = "height";
	public static final String IMAGE_NAME = "imagepath";
	public static final String TIMES_NAME = "times";
	public static final String ANGLE_NAME = "angle";
	
	/**
	 * private part
	 */
	private static final ModelContants contants = new ModelContants();
	
	private IContentReader reader = null;
	
	//private String oldName = "";
	
	//private Entity oldEntity = null;
	
	@Override
	public int getIntContent(String nodeName, String attributeName) throws MaEditorXMLException {
		if (nodeName == null || attributeName == null) return ViewContants.ILLEGAL_INTEGER_VALUE;
		else return reader.getIntContent(nodeName, attributeName);
	}
	
	@Override
	public Node getContentNode(String nodeName) {
		if (nodeName == null) return null;
		else return reader.getContentNode(nodeName);
	}
	
	@Override
	public String getContent(String nodeName, String attributeName) throws MaEditorXMLException {
		if (nodeName == null || attributeName == null) return null;
		else return reader.getContent(nodeName, attributeName);
	}
	
	@Override
	public double getDoubleContent(String nodeName, String attributeName)
			throws MaEditorXMLException {
		if (nodeName == null || attributeName == null) return ViewContants.ILLEGAL_DOUBLE_VALUE;
		else return reader.getDoubleContent(nodeName, attributeName);
	}
	
	@Override
	public void refresh() {
		reader.refresh();
	}
	
	/**
	 * singleton the same as viewcontants class
	 * {@link ViewContants}
	 */
	private ModelContants() {
		try {
			init();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @throws FileNotFoundException
	 */
	private void init() throws FileNotFoundException {
		reader = new ContentXMLReader(ViewContants.CONFIG_ASSERT + ENTITY_CONTANTS_PATH);
	}
	
	/**
	 * 
	 * @return the only references
	 */
	public static ModelContants getNewInstance() {
		return ModelContants.contants;
	}
	
	/**
	 * 
	 * @param name
	 * @return entity init by xml
	 * @throws MaEditorXMLException 
	 */
	public IPainter initConfig(final IPainter entity) throws MaEditorXMLException {	
		String name = entity.getName();
		int x, y, id, width, height;
		double times, angle;
		
		if ((x = this.getIntContent(name, X_NAME)) != ViewContants.ILLEGAL_INTEGER_VALUE) entity.setX(x);
		if ((y = this.getIntContent(name, Y_NAME)) != ViewContants.ILLEGAL_INTEGER_VALUE) entity.setY(y);
		if ((id = this.getIntContent(name, ID_NAME)) != ViewContants.ILLEGAL_INTEGER_VALUE) entity.setID(id);
		if ((width = this.getIntContent(name, WIdTH_NAME)) != ViewContants.ILLEGAL_INTEGER_VALUE) entity.setWidth(width);
		if ((height = this.getIntContent(name, HEIGHT_NAME)) != ViewContants.ILLEGAL_INTEGER_VALUE) entity.setHeight(height);
		if ((times = this.getDoubleContent(name, TIMES_NAME)) != ViewContants.ILLEGAL_DOUBLE_VALUE) entity.setTimes(times);
		if ((angle = this.getDoubleContent(name, ANGLE_NAME)) != ViewContants.ILLEGAL_DOUBLE_VALUE) entity.setAngle(angle);
		
		return entity;
	}
}
